<?php

session_start();

if (!isset($_SESSION['ha_usr']) || !isset($_SESSION['ha_rol'])) {

    header("Location: ../index.php");
}


require_once '../MyDB.php';
$con = new MyDB();

$type = $_POST["type"];

if ($type == 1) {
    // Inserta
    $nombre = $_POST["nombre"];
    $apellido = $_POST["apellido"];
    $mail = $_POST["mail"];
    $nacionalidad = $_POST["nacionalidad"];
    $comentarios = $_POST["comentarios"];
    $tipodoc = $_POST["tipodoc"];
    $nrodoc = $_POST["nrodoc"];
    $marcado = $_POST["marcado"];

    $query = $con->insertInto("clientes", $fields = array("Nombre" => $nombre, "Apellido" => $apellido, "Mail" => $mail, "IdNacionalidad" => $nacionalidad,
        "Comentarios" => $comentarios, "TipoDoc" => $tipodoc, "NroDoc" => $nrodoc, "Marcado" => $marcado));

//    print_r($query["sql"]);

    $result = $query["status"];
    if ($result == "success") {
        echo 'Dato guardado con éxito';
    } else {
        echo "El dato no ha sido guardado";
    }
} elseif ($type == 0 || $type == 4) {
    // Búsqueda - 0
    // Búsqueda con radio - 4

    $_orderBy = $_POST["orderby"];


    $nombre = $_POST["nombre"];
    $apellido = $_POST["apellido"];
    $mail = $_POST["mail"];
    $tipodoc = $_POST["tipodoc"];
    $nrodoc = $_POST["nrodoc"];


    // Condiciones de búsqueda
    $sqlAux = '';

    if (!isset($nombre) || trim($nombre) === '' || $nombre === NULL) {
        $sqlAux = $sqlAux . " ";
    } else {
        $sqlAux = $sqlAux . " and Nombre like '%" . $nombre . "%' ";
    }

    if (!isset($apellido) || trim($apellido) === '' || $apellido === NULL) {
        $sqlAux = $sqlAux . " ";
    } else {
        $sqlAux = $sqlAux . " and Apellido like '%" . $apellido . "%' ";
    }

    if (!isset($mail) || trim($mail) === '' || $mail === NULL) {
        $sqlAux = $sqlAux . " ";
    } else {
        $sqlAux = $sqlAux . " and Mail like '%" . $mail . "%' ";
    }

    if (!isset($nrodoc) || trim($nrodoc) === '' || $nrodoc === NULL) {
        $sqlAux = $sqlAux . " ";
    } else {
        $sqlAux = $sqlAux . " and NroDoc like '%" . $nrodoc . "%' ";
    }

    //$query = $con->selectFrom("clientes", $columns = null, $where = null, $like = false, $orderby = null, $direction = "DESC", $limit = null, $offset = null);

    $SQL = "SELECT `IdCliente`, `Nombre`, `Apellido`, `Mail`, (SELECT  concat(id, ' - ', country_name) as country "
            . "FROM  `countries` WHERE id =`IdNacionalidad`) as Nacionalidad, `TipoDoc`, `NroDoc`, `Comentarios`, "
            . "IF(`Marcado` =0, 'No', 'Si') as Marcado FROM `clientes` WHERE 1" . $sqlAux;

    $query = $con->customSelectFrom($SQL);

    if ($query["num"] > 0) {
        $result = $query["result"];
        foreach ($result as $row) {


            $param = "'" . $row["IdCliente"] . "','" . $row["Nombre"] . "','" . $row["Apellido"] . "','" . $row["Mail"] . "','"
                    . $row["Nacionalidad"] . "','" . $row["Comentarios"] . "','" . $row["TipoDoc"] . "','" . $row["NroDoc"] . "','"
                    . $row["Marcado"] . "'";

            $backMarcado = '';
            if ($row["Marcado"] == 'Si') {
                $backMarcado = ' style = "color:red;" ';
            }

            if ($type == 0) {
                echo "<tr" . $backMarcado . "><td>" . $row["IdCliente"] . "</td>"
                . "<td>" . $row["Nombre"] . "</td><td>" . $row["Apellido"] . "</td>"
                . "<td>" . $row["Mail"] . "</td><td>" . $row["Nacionalidad"] . "</td>"
                . "<td>" . $row["Comentarios"] . "</td><td>" . $row["TipoDoc"] . "</td>"
                . "<td>" . $row["NroDoc"] . "</td><td>" . $row["Marcado"] . "</td>"
                . "<td onClick='deleteCliente(" . $row["IdCliente"] . ")' "
                . "style='cursor:pointer;'><img style='height:20px; width:20px;' src='./img/delete.png'></td>"
                . '<td onClick="window.pantalla=\'clientes\';selectCliente(' . $param . ');" '
                . "style='cursor:pointer;'><img style='height:20px; width:20px;' src='./img/edit.png'></td></tr>";
            } else {
                $idRowSelected = "'tmpClient_" . $row["IdCliente"] . "'";
                $fnc = "";
                echo '<tr onClick="selectClientePerm(' . $param . ',' . $idRowSelected . ')" id=' . $idRowSelected . ' ' . $backMarcado . '>'
                . '<td>' . $row["IdCliente"] . '</td>'
                . "<td>" . $row["Nombre"] . "</td><td>" . $row["Apellido"] . "</td>"
                . "<td>" . $row["Mail"] . "</td><td>" . $row["Nacionalidad"] . "</td>"
                . "<td>" . $row["Comentarios"] . "</td><td>" . $row["TipoDoc"] . "</td>"
                . "<td>" . $row["NroDoc"] . "</td><td>" . $row["Marcado"] . "</td>"
                . '<td '
                . "style='cursor:pointer;'></td></tr>";
            }
        }
    } else {
        echo '<tr >'
        . '<td></td>'
        . "<td></td><td></td>"
        . "<td></td><td></td>"
        . "<td></td><td></td>"
        . "<td></td><td></td>"
        . '<td '
        . "style='cursor:pointer;'></td></tr>";
    }
} elseif ($type == 2) {
    // Borra
    $id = $_POST["id"];
    $query = $con->deleteFrom("clientes", $where = array("IdCliente" => $id), $like = false, $limit = 1);
    $result = $query["status"];

    //print_r($query['sql']);
    if ($result == "success") {
        echo 'Dato borrado con éxito';
    } else {
        echo "El dato no ha sido borrado";
    }
} elseif ($type == 3) {
    // Modificar
    $id = $_POST["id"];
    $nombre = $_POST["nombre"];
    $apellido = $_POST["apellido"];
    $mail = $_POST["mail"];
    $nacionalidad = $_POST["nacionalidad"];
    $comentarios = $_POST["comentarios"];
    $tipodoc = $_POST["tipodoc"];
    $nrodoc = $_POST["nrodoc"];
    $marcado = $_POST["marcado"];

    $query = $con->updateTable("clientes", $fields = array("Nombre" => $nombre, "Apellido" => $apellido, "Mail" => $mail, "IdNacionalidad" => $nacionalidad,
        "Comentarios" => $comentarios, "TipoDoc" => $tipodoc, "NroDoc" => $nrodoc, "Marcado" => $marcado), $where = array("IdCliente" => $id));
    $result = $query["status"];
    if ($result == "success") {
        echo 'Dato guardado con éxito';
    } else {
        echo "El dato no ha sido guardado";
    }
} elseif ($type == 5) {
    getClientById();
}

function getClientById() {
    // Búsqueda - 5 - por id y json
    require_once '../src/MyDBi.php';
    $db = new MysqliDb();
    
    
    
    $id = $_POST["id"];
    //$query = $con->selectFrom("clientes", $columns = null, $where = null, $like = false, $orderby = null, $direction = "DESC", $limit = null, $offset = null);
    $SQL = "SELECT `IdCliente`, `Nombre`, `Apellido`, `Mail`, (SELECT  concat(id, ' - ', country_name) as country "
            . "FROM  `countries` WHERE id =`IdNacionalidad`) as Nacionalidad, `TipoDoc`, `NroDoc`, `Comentarios`, "
            . "IF(`Marcado` =0, 'No', 'Si') as Marcado FROM `clientes` WHERE IdCliente=" . $id;

    $result = $db->rawQuery($SQL);

    if ($db->count > 0) {
        
        foreach ($result as $row) {


            $param = "" . $row["IdCliente"] . " " . $row["Nombre"] . " " . $row["Apellido"] . " " . $row["Mail"] . " "
                    . $row["TipoDoc"] . " " . $row["NroDoc"] . " "
                    . $row["Marcado"] . "";
            echo $param;
        }
    }
}

?>